import Cookies from "js-cookie";

const TokenKey = "Admin-Token";

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  return Cookies.set(TokenKey, token);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

export function getName() {
  return Cookies.get("username");
}

export function setName(token) {
  return Cookies.set("username", token);
}

export function removeName() {
  return Cookies.remove("username");
}

export function getId() {
  return Cookies.get("userid");
}

export function setId(token) {
  return Cookies.set("userid", token);
}

export function removeId() {
  return Cookies.remove("userid");
}
export function setOrg(token){
  return Cookies.set('org',JSON.stringify(token))
}
export function getOrg(){
  return JSON.parse(Cookies.get('org'))
}
export function removeOrg(){
  return Cookies.remove('org')
}
//权限设置
export function getAuthority() {
  return Cookies.get("authority");
}

export function setAuthority(data) {
  return Cookies.set("authority", data);
}

export function removeAuthority() {
  return Cookies.remove("authority");
}
export function exportFile(data, type, fileName) {
  const blob = new Blob([data], {
    // type类型后端返回来的数据中会有，根据自己实际进行修改
    // 表格下载为 application/xlsx，压缩包为 application/zip等，
    type: type,
  });
  const filename = fileName;
  if (typeof window.navigator.msSaveBlob !== "undefined") {
    window.navigator.msSaveBlob(blob, filename);
  } else {
    var blobURL = window.URL.createObjectURL(blob);
    // 创建隐藏<a>标签进行下载
    var tempLink = document.createElement("a");
    tempLink.style.display = "none";
    tempLink.href = blobURL;
    tempLink.setAttribute("download", filename);
    if (typeof tempLink.download === "undefined") {
      tempLink.setAttribute("target", "_blank");
    }
    document.body.appendChild(tempLink);
    tempLink.click();
    document.body.removeChild(tempLink); // 移除dom元素
    window.URL.revokeObjectURL(blobURL); // 释放bolb内存
  }
}
